package com.epoint.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.epoint.entity.BookInfo;
import com.epoint.service.BookInfoService;

/**
 * Servlet implementation class BookInfoListAction
 */
@WebServlet("/bookinfolistaction")
public class BookInfoListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	BookInfoService bookInfoListService = new BookInfoService();

	public BookInfoListAction() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String method = request.getParameter("method");

		if ("queryAll".equals(method)) {
			List<BookInfo> infos = bookInfoListService.getAll();
			response.getWriter().println(JSONObject.toJSONStringWithDateFormat(infos, "yyyy-MM-dd"));
		} else {
			String bookName = request.getParameter("bookName");
			String bookType = request.getParameter("bookType");
			String pageIndex = request.getParameter("pageIndex");
			String pageSize = request.getParameter("pageSize");
			String sortField = request.getParameter("sortField");
			String sortOrder = request.getParameter("sortOrder");

			// 请求查询方法
			List<BookInfo> bookInfos = bookInfoListService.getBookList(bookName, bookType, Integer.valueOf(pageIndex),
					Integer.valueOf(pageSize), sortField, sortOrder);

			// 总数
			int count = bookInfoListService.getBookListCount(bookName, bookType);
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("total", count);
			resultMap.put("data", bookInfos);
			// 返回构建的json数据
			response.getWriter().println(JSONObject.toJSONString(resultMap));
		}
	}

}
